package com.qb.video.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.widget.LinearLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.just.agentweb.*
import com.qb.baselibrary.basekotlin.BaseActivity
import com.qb.baselibrary.basekotlin.EmptyViewModel
import com.qb.common.router.RouterPath
import com.qb.video.R
import com.qb.video.databinding.VideoActivityWebBinding

@Route(path = RouterPath.URL_Video_WebActivity)
class WebActivity : BaseActivity<EmptyViewModel,VideoActivityWebBinding>() {
    var headers:Map<String,String>?=null
    var z:String?=null
    var s1ig:String?=null
    var g:String?=null
    var subUrl:String?=null
    override fun initView(savedInstanceState: Bundle?) {
        var mAgentWeb = AgentWeb.with(this).setAgentWebParent(mBinding.llContainer,-1, LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
            .useDefaultIndicator(-1, 3)//设置进度条颜色与高度，-1为默认值，高度为2，单位为dp。
            .setWebViewClient(mWebViewClient)
            .setWebChromeClient(mWebChromeClient) //WebChromeClient
            .setMainFrameErrorView(R.layout.agentweb_error_page, -1)
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)//打开其他页面时，弹窗质询用户前往其他应用 AgentWeb 3.0.0 加入。
            .interceptUnkownUrl() //拦截找不到相关页面的Url AgentWeb 3.0.0 加入。
            .createAgentWeb()//创建AgentWeb。
            .ready()//设置 WebSettings。
            .go(getUrl()); //WebView载入该url地址的页面并显示。
        AgentWebConfig.debug()
        mAgentWeb.webCreator.webView.overScrollMode = WebView.OVER_SCROLL_NEVER
    }
    private fun getUrl():String{
        return "https://z1.m1907.cn/";
    }
    override fun initData() {
    }
    var mWebViewClient: WebViewClient? = object : WebViewClient() {
        override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
            super.onReceivedError(view, request, error)
        }


        @SuppressLint("NewApi")
        override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
            Log.e("InternetActivity", request.toString() + "  "+request.url)
            var url:String? = request.url.toString()
            LogUtils.e("headers-->"+request.requestHeaders)
            headers = request.requestHeaders
            url?.let {
                if (url.contains("m1907.cn/api/v/")) {
                    z = getValueByName(url, "z")
                    s1ig = getValueByName(url, "s1ig")
                    g = getValueByName(url, "g")
                    LogUtils.e("z " + z)
                    LogUtils.e("s1ig " + s1ig)
                    LogUtils.e("g " + g)
                    var apiIndex = url.indexOf("api/v")
                    subUrl = url.substring(0,apiIndex)
                    LogUtils.e("==-->截取url $subUrl")
//                    if (Env.videoUrl!=subUrl){
//                        Env.videoUrl = subUrl as String
//                    }
                }
            }
            return super.shouldInterceptRequest(view, request)
        }
        override fun onReceivedHttpError(
            view: WebView,
            request: WebResourceRequest,
            errorResponse: WebResourceResponse
        ) {
            super.onReceivedHttpError(view, request, errorResponse)
        }

        override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
            super.onReceivedError(view, errorCode, description, failingUrl)
        }
    }
    var mWebChromeClient: WebChromeClient? = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
        }

        override fun onReceivedTitle(view: WebView, title: String) {
            super.onReceivedTitle(view, title)
        }
    }
    /***
     * 获取url 指定name的value;
     * @param url
     * @param name
     * @return
     */
    fun getValueByName(url:String,name:String):String{
        var result = ""
        var index  = url.indexOf("?")
        var temp = url.substring(index+1)
        var keyValue = temp.split("&")
        for (index in keyValue.indices){
            var every = keyValue[index]
            if (every.contains(name)){
                result = every.replace(name+"=","")
                break
            }
        }
        return result
    }
}